package com.example.module_repair

import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.base.BaseActivity
import com.example.module_repair.databinding.ActivityRepairBinding
import com.example.module_repair.repairnet.RepairAdapter
import com.example.module_repair.repairnet.RepairModel
import com.example.module_repair.repairnet.RepairViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Route(path = "/repair/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<RepairViewModel, ActivityRepairBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_repair
    private val repairviewmodel: RepairViewModel by viewModels()
    lateinit var repairAdapter: RepairAdapter
    var repairlist: MutableList<RepairModel> = mutableListOf()

    override fun initView() {
        super.initView()
    }

    override fun initData() {
        super.initData()
//        lifecycleScope.launch {
//            repairviewmodel.repairOrdsFlow.collect { it ->
//                if (it.isNotEmpty()) {
//                     repairlist.addAll(it)
//                    launch {
//                        mBinding.progress.visibility = View.VISIBLE
//                        delay(500)
//                        mBinding.progress.visibility = View.GONE
//                        Log.d("qqq", "initData: " + "成功")
//                    }
//                }
////            }
//        }



        //点击保修登记
        mBinding.floatactionbar.setOnClickListener {
            ARouter.getInstance().build("/repair/CreatrRepairOrdersActivity").navigation()
        }
        mBinding.repairTop.back.setOnClickListener {
            finish()
        }
    }

    override fun initListener() {
        super.initListener()
    }

    override fun onResume() {
        super.onResume()
        repairviewmodel.getRepairOrders()
        repairviewmodel.repairOrds.observe(this) {
            if (it.isNotEmpty()) {
                lifecycleScope.launch {
                    mBinding.progress.visibility = View.VISIBLE
                    delay(500)
                    mBinding.progress.visibility = View.GONE
                    repairlist.addAll(it)
                    repairlist.reverse()
                    repairAdapter = RepairAdapter()
                    repairAdapter.setData(repairlist)
                    mBinding.rvrepair.adapter = repairAdapter
                    mBinding.rvrepair.layoutManager =
                        androidx.recyclerview.widget.LinearLayoutManager(this@RepairActivity)
                }


            }
        }

    }
}